package com.zshlong.seckill.util;


import com.zshlong.seckill.pojo.SeckillUser;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 创建虚拟用户注入数据库用于测试
 * */
public class UserUtil {

	private static void createUser(int count) throws Exception {
		List<SeckillUser> users = new ArrayList<SeckillUser>(count);
		//生成用户
		for (int i = 0; i < count; i++) {
			SeckillUser user = new SeckillUser();
			user.setId(13000000000L + i);
			user.setNickname("user" + i);
			user.setSalt("1a2b3c");
			user.setPassword(MD5Util.inputPassToDbPass("123456", user.getSalt()));
			user.setHead("123");
			user.setRegisterDate(new Date());
			user.setLastLoginDate(new Date());
			user.setLoginCount(1);
			user.setDeliveryAddress("北京故宫博物馆对面天桥上的第"+i+"号地摊");
			users.add(user);
		}
		System.out.println("create user");


		//插入数据库
		Connection conn = DBUtil.getConn();
		String sql = "insert into seckill.miaosha_user(id, nickname, password, salt, head, register_date,last_login_date,login_count,delivery_address)values(?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for(int i=0;i<users.size();i++) {
			SeckillUser user = users.get(i);
			pstmt.setLong(1, user.getId());
			pstmt.setString(2, user.getNickname());
			pstmt.setString(3, user.getPassword());
			pstmt.setString(4, user.getSalt());
			pstmt.setString(5, user.getHead());
			pstmt.setTimestamp(6, new Timestamp(user.getRegisterDate().getTime()));
			pstmt.setTimestamp(7, new Timestamp(user.getLastLoginDate().getTime()));
			pstmt.setInt(8,user.getLoginCount());
			pstmt.setString(9,user.getDeliveryAddress());
			pstmt.addBatch();
		}
		pstmt.executeBatch();
		pstmt.close();
		conn.close();
		System.out.println("insert to db");

		//登录，生成token
		String urlString = "http://localhost:8080/login/doLogin";
		File file = new File("F:/tokens.txt");
		if (file.exists()) {
			file.delete();
		}
		RandomAccessFile raf = new RandomAccessFile(file, "rw");
		file.createNewFile();
		raf.seek(0);
		for (int i = 0; i < users.size(); i++) {
			SeckillUser user = users.get(i);
			URL url = new URL(urlString);
			HttpURLConnection co = (HttpURLConnection) url.openConnection();
			co.setRequestMethod("POST");
			co.setDoOutput(true);
			OutputStream out = co.getOutputStream();
			String params = "mobile=" + user.getId() + "&password=" + MD5Util.inputPassToFormPass("123456");
			out.write(params.getBytes());
			out.flush();
			InputStream inputStream = co.getInputStream();
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = inputStream.read(buff)) >= 0) {
				bout.write(buff, 0, len);
			}
			inputStream.close();
			bout.close();
			String response = new String(bout.toByteArray());
			JSONObject jo = JSON.parseObject(response);
			String token = jo.getString("data");
			System.out.println("create token : " + user.getId());

			String row = user.getId() + "," + token;
			raf.seek(raf.length());
			raf.write(row.getBytes());
			raf.write("\r\n".getBytes());
			System.out.println("write to file : " + user.getId());
		}
		raf.close();

		System.out.println("over");
	}

	public static void main(String[] args) throws Exception {
		createUser(10000);
	}
}
